﻿using UnityEngine;
using System.Collections;

public class SimpleAB : MonoBehaviour 
{
    public string abName = "cube_ab.ab1";

    //不同平台下StreamingAssets的路径是不同的，这里需要注意一下。
    public string PathURL;
    
    void Awake()
    {
        PathURL = 
        #if UNITY_ANDROID
		"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
		Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
 "file://" + Application.dataPath + "/StreamingAssets/";
#else
        string.Empty;
#endif
    }

    private IEnumerator LoadMainGameObject(string path)
    {
        WWW bundle = new WWW(path);

        yield return bundle;

        //加载到游戏中
        yield return Instantiate(bundle.assetBundle.mainAsset);

        bundle.assetBundle.Unload(false);
    }

    void OnGUI()
    {
        if (GUILayout.Button("Load one Assetbundle"))
        {
            StartCoroutine(LoadMainGameObject(PathURL + abName));
        }
    }
}
